package com.bs.alibabacontentconsumer.service.impl;

import com.bs.alibabacontentconsumer.feign.AccountOpenFeignClients;
import com.bs.alibabacontentconsumer.mapper.AccountMapper;
import com.bs.alibabacontentconsumer.pojo.Account;
import com.bs.alibabacontentconsumer.service.AccountService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;

/**
 * @Author: Tvemic⁹
 * @DateTime: 2022/7/5 9:29
 * @Description: TODO
 */

@Service
public class AccountServiceImpl implements AccountService {

    @Resource
    AccountMapper accountMapper;

    @Resource
    AccountOpenFeignClients accountOpenFeignClients;

    @GlobalTransactional
    @Override
    public void updateAccountInfo(Account account) {

        Example example = new Example(Account.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("accountNo", account.getAccountNo());
        Account ac = accountMapper.selectOneByExample(example);
        Integer amount = ac.getAccountBalance() - account.getAccountBalance();
        account.setAccountBalance(amount);
        // 张三扣减金额
        accountMapper.updateByExampleSelective(account, example);

        // 李四增加金额
        accountOpenFeignClients.updateAccountInfo(account);

        //error 测试事务一致性，出错即回滚
        int i = 1/0;
    }
}
